{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LFortran: Interactive Fortran Compiler\n",
    "\n",
    "This Demo notebook shows available magic commands in LFortran and how to use them."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Magic Commands"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Initialize some variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "integer :: i, j, n\n",
    "n = 5\n",
    "j = 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## AST"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Show the Abstract Syntax Tree (AST) after parsing (based on syntax only, no semantics):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%showast\n",
    "do i = 1, n\n",
    "    j = j + i\n",
    "end do"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ASR\n",
    "\n",
    "Show the Abstract Semantic Representation (ASR), which contains all the semantics."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%showasr\n",
    "do i = 1, n\n",
    "    j = j + i\n",
    "end do"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## LLVM\n",
    "\n",
    "Show LLVM code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%showllvm\n",
    "do i = 1, n\n",
    "    j = j + i\n",
    "end do"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ASM\n",
    "\n",
    "Show assembly code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%showasm\n",
    "do i = 1, n\n",
    "    j = j + i\n",
    "end do"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## C++\n",
    "\n",
    "Transform Fortran code to C++"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%showcpp\n",
    "subroutine triad(a, b, scalar, c)\n",
    "real, intent(in) :: a(:), b(:), scalar\n",
    "real, intent(out) :: c(:)\n",
    "integer :: N, i\n",
    "N = size(a)\n",
    "do concurrent (i = 1:N)\n",
    "    c(i) = a(i) + scalar * b(i)\n",
    "end do\n",
    "end subroutine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%showcpp\n",
    "program doconcurrentloop_01\n",
    "implicit none\n",
    "real, dimension(10000) :: a, b, c\n",
    "real :: scalar\n",
    "integer :: i, nsize\n",
    "scalar = 10\n",
    "nsize = size(a)\n",
    "do concurrent (i = 1:nsize)\n",
    "    a(i) = 5\n",
    "    b(i) = 5\n",
    "end do\n",
    "call triad(a, b, scalar, c)\n",
    "print *, \"End Stream Triad\"\n",
    "\n",
    "contains\n",
    "\n",
    "    subroutine triad(a, b, scalar, c)\n",
    "    real, intent(in) :: a(:), b(:), scalar\n",
    "    real, intent(out) :: c(:)\n",
    "    integer :: N, i\n",
    "    N = size(a)\n",
    "    do concurrent (i = 1:N)\n",
    "        c(i) = a(i) + scalar * b(i)\n",
    "    end do\n",
    "    end subroutine\n",
    "\n",
    "end program"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Format Fortran code\n",
    "\n",
    "You can use `lfortran fmt` to format Fortran code, or the `%%showfmt` command."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%showfmt\n",
    "subroutine triad(a, b, scalar, c); real, intent(in) :: a(:), b(:), scalar; real, intent(out) :: c(:); integer :: N, i; N = size(a); do concurrent (i = 1:N); c(i) = a(i) + scalar * b(i); end do; end subroutine"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Fortran",
   "language": "fortran",
   "name": "fortran"
  },
  "language_info": {
   "file_extension": ".f90",
   "mimetype": "text/x-fortran",
   "name": "fortran",
   "version": "2018"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
